//
//  PingTransition.m
//  KYPingTransition
//
//  Created by koki on 15/11/10.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "PingTransition.h"


@interface PingTransition ()
@property (nonatomic,strong)id<UIViewControllerContextTransitioning> transitionContext;

@end

@implementation PingTransition

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
    return  0.7f;
}

-(CGRect)startRect
{
    if (_startRect.size.width==0 && _startRect.size.height==0)
    {
        _startRect=CGRectMake(0, 0, 10, 10);
    }
    return _startRect;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    self.transitionContext = transitionContext;

    UIViewController * fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *contView = [transitionContext containerView];

    
    UIBezierPath *maskStartBP =  [UIBezierPath bezierPathWithOvalInRect:self.startRect];
    [contView addSubview:fromVC.view];
    [contView addSubview:toVC.view];


    CGPoint finalPoint;
    CGPoint centerPoint=CGPointMake(self.startRect.origin.x+self.startRect.size.width/2.0, self.startRect.origin.y+self.startRect.size.height/2.0);
    if(self.startRect.origin.x > (toVC.view.bounds.size.width / 2))
    {
        if (self.startRect.origin.y < (toVC.view.bounds.size.height / 2)) {
            finalPoint = CGPointMake(centerPoint.x - 0, centerPoint.y - CGRectGetMaxY(toVC.view.bounds)+30);
        }else{
            finalPoint = CGPointMake(centerPoint.x - 0, centerPoint.y - 0);
        }
    }else{
        if (self.startRect.origin.y < (toVC.view.bounds.size.height / 2)) {
            
            finalPoint = CGPointMake(centerPoint.x - CGRectGetMaxX(toVC.view.bounds), centerPoint.y - CGRectGetMaxY(toVC.view.bounds)+30);
        }else{
            finalPoint = CGPointMake(centerPoint.x - CGRectGetMaxX(toVC.view.bounds), centerPoint.y - 0);
        }
    }
    
    
    
    CGFloat radius = sqrt((finalPoint.x * finalPoint.x) + (finalPoint.y * finalPoint.y));
    UIBezierPath *maskFinalBP = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.startRect, -radius, -radius)];
    
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = maskFinalBP.CGPath;
    toVC.view.layer.mask = maskLayer;
    
    
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.fromValue = (__bridge id)(maskStartBP.CGPath);
    maskLayerAnimation.toValue = (__bridge id)((maskFinalBP.CGPath));
    maskLayerAnimation.duration = [self transitionDuration:transitionContext];
    maskLayerAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    maskLayerAnimation.delegate = self;
    
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
    
    
}


#pragma mark - CABasicAnimation的Delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

    [self.transitionContext completeTransition:![self. transitionContext transitionWasCancelled]];
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;

}



@end





